package com.xvjialing.lytech.weatherreport.controller;

import com.xvjialing.lytech.weatherreport.service.CityDataService;
import com.xvjialing.lytech.weatherreport.service.WeatherReportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/report")
public class WeatherReportController {
    private Logger logger = LoggerFactory.getLogger(WeatherReportController.class);

    @Autowired
    WeatherReportService weatherReportService;

    @Autowired
    CityDataService cityDataService;

    @GetMapping("/cityCode/{cityCode}")
    public ModelAndView getReportByCityCode(Model model, @PathVariable("cityCode") String cityCode){
        logger.info("cityCode:{}",cityCode);
        
        model.addAttribute("title","weather report");
        model.addAttribute("cityCode",cityCode);
        model.addAttribute("cityList",cityDataService.getCityDataList());
        model.addAttribute("report",weatherReportService.getWeatherByCityCode(cityCode));
        return new ModelAndView("weather/report","reportModel",model);
    }

}